# -*- coding: utf-8 -*-
"""
Created on Sun Nov 11 02:53:59 2018

@author: Administrator
"""
class LNode:
    def __init__(self, element, next_ = None):
        self.element = element
        self.next = next_
        
        

class LinkedQueue:
    """带头尾指针的单链表队列
    """
    def __init__(self):
        self.head = None
        self.rear = None
        self.length = 0
        
    def is_empty(self):
        if self.length == 0:
            return True
        return False
    
    def enqueue(self, element):
        """元素入队
        """
        # 如果没有元素
        node = LNode(element)
        if self.is_empty():
            self.head = node
            self.rear = node
            self.length += 1
        else:
            # 如果有元素，尾插法
            self.rear.next = node
            self.rear = node
            self.length += 1
    
    def dequeue(self):
        """元素出队
        """
        if self.is_empty():
            raise ValueError("queque empty!")
        elif self.head.next is None:
            self.head = self.rear = None
            self.length -= 1
        else:
            self.head = self.head.next
            self.length -= 1
    
    def peek(self):
        """查看队首元素，不删除
        """
        if self.is_empty():
            return None
        return self.head.element
    
    def print_all(self):
        result = []
        p = self.head
        while p is not None:
            result.append(p.element)
            p = p.next
        return result
        